﻿// ---------------------------------------------------------- ----------------------------------------------------------
// <copyright file="VehicleTransactionEditorViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the VehicleTransactionEditorViewModel type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.Client.ViewModel
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.Contracts;
    using System.Linq;
    using System.Windows.Data;
    using Business.Interfaces;
    using Common;
    using Hisaab.Common;

    using Microsoft.Practices.ServiceLocation;

    using Model;

    /// <summary>
    /// Defines the <seealso cref="VehicleTransactionEditorViewModel"/> type.
    /// </summary>
    public class VehicleTransactionEditorViewModel : ViewModelBase
    {
        /// <summary>
        /// Save command
        /// </summary>
        public static readonly ActiveAwareCommand SaveCommand = new ActiveAwareCommand();

        /// <summary>
        /// Account object
        /// </summary>
        private readonly IVehicleTransactionManager _vehicleTransactionManager;

        /// <summary>
        /// Account manager
        /// </summary>
        private readonly IAccountManager _accountManager;

        /// <summary>
        /// Type manager
        /// </summary>
        private readonly IVehicleManager _vehicleManager;

        /// <summary>
        /// The session manager
        /// </summary>
        private readonly ISession _session;

        /// <summary>
        /// VehicleTransaction types
        /// </summary>
        private CollectionView _vehicleTransactionTypes;

        /// <summary>
        /// Accounts list
        /// </summary>
        private CollectionView _accounts;

        /// <summary>
        /// Selected VehicleTransaction
        /// </summary>
        private VehicleTransaction _selectedVehicleTransaction;

        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleTransactionEditorViewModel"/> class.
        /// </summary>
        /// <param name="vehicleTransaction">The VehicleTransaction.</param>
        public VehicleTransactionEditorViewModel(VehicleTransaction vehicleTransaction)
            : this(
                ServiceLocator.Current.GetInstance<IVehicleTransactionManager>(),
                ServiceLocator.Current.GetInstance<IAccountManager>(),
                ServiceLocator.Current.GetInstance<IVehicleManager>(),
                ServiceLocator.Current.GetInstance<ISession>(),
                vehicleTransaction)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="VehicleTransactionEditorViewModel"/> class.
        /// </summary>
        /// <param name="vehicleTransactionManager">The VehicleTransaction manager.</param>
        /// <param name="accountManager">The account manager</param>
        /// <param name="vehicleManager">The tag manager</param>
        /// <param name="session">The session</param>
        /// <param name="vehicleTransaction">The VehicleTransaction.</param>
        public VehicleTransactionEditorViewModel(
            IVehicleTransactionManager vehicleTransactionManager,
            IAccountManager accountManager,
            IVehicleManager vehicleManager,
            ISession session,
            VehicleTransaction vehicleTransaction)
        {
            Contract.Requires<ArgumentNullException>(accountManager != null);
            Contract.Requires<ArgumentNullException>(vehicleTransactionManager != null);
            Contract.Requires<ArgumentNullException>(vehicleManager != null);
            Contract.Requires<ArgumentNullException>(session != null);

            _vehicleTransactionManager = vehicleTransactionManager;
            _accountManager = accountManager;
            _vehicleManager = vehicleManager;
            _session = session;
            IsNewVehicleTransaction = vehicleTransaction == null;
            SelectedVehicleTransaction = vehicleTransaction ?? new VehicleTransaction { Date = DateTime.Today };
        }

        /// <summary>
        /// Gets the VehicleTransactions.
        /// </summary>
        public IEnumerable<VehicleTransaction> VehicleTransactions
        {
            get { return _vehicleTransactionManager.List().Select(x => x.Map<Business.Entities.VehicleTransaction, VehicleTransaction>()); }
        }

        /// <summary>
        /// Gets the Account Types.
        /// </summary>
        public CollectionView Vehicles
        {
            get
            {
                if (_vehicleTransactionTypes == null)
                {
                    _vehicleTransactionTypes = new CollectionView(
                        _vehicleManager
                        .List(new Business.Entities.Vehicle { UserId = _session.LoggedUser })
                        .Select(x => x.Map<Business.Entities.Vehicle, Vehicle>()));
                }

                return _vehicleTransactionTypes;
            }
        }

        /// <summary>
        /// Gets the Account Types.
        /// </summary>
        public CollectionView Accounts
        {
            get
            {
                if (_accounts == null)
                {
                    _accounts = new CollectionView(
                        _accountManager
                        .List(new Business.Entities.Account { UserId = _session.LoggedUser })
                        .Select(x => x.Map<Business.Entities.Account, Account>()));
                }

                return _accounts;
            }
        }

        /// <summary>
        /// Gets or sets the selected VehicleTransaction.
        /// </summary>
        /// <value>
        /// The selected VehicleTransaction.
        /// </value>
        public VehicleTransaction SelectedVehicleTransaction
        {
            get
            {
                return _selectedVehicleTransaction;
            }

            set
            {
                if (_selectedVehicleTransaction != value)
                {
                    _selectedVehicleTransaction = value;

                    OnPropertyChanged("SelectedVehicleTransaction");
                }
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [new account].
        /// </summary>
        /// <value>
        /// True if new account; otherwise, false.
        /// </value>
        public bool IsNewVehicleTransaction { get; set; }

        /// <summary>
        /// Saves the VehicleTransaction.
        /// </summary>
        /// <param name="obj">The object</param>
        public void SaveVehicleTransaction(object obj)
        {
            if (IsNewVehicleTransaction)
            {
                _vehicleTransactionManager.Add(SelectedVehicleTransaction.Map<VehicleTransaction, Business.Entities.VehicleTransaction>());
            }
            else
            {
                _vehicleTransactionManager.Update(SelectedVehicleTransaction.Map<VehicleTransaction, Business.Entities.VehicleTransaction>());
            }

            DocumentManager.Instance.CloseCurrent();
        }

        /// <summary>
        /// Determines whether this instance [can save VehicleTransaction] the specified obj.
        /// </summary>
        /// <param name="obj">The object</param>
        /// <returns>
        /// True if this instance can save VehicleTransaction the specified obj; otherwise, false.
        /// </returns>
        public bool CanSaveVehicleTransaction(object obj)
        {
            return true;
        }
    }
}